package com.goulin.sprmvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.support.RequestContextUtils;
//拦截器
public class DemoInterceptor extends HandlerInterceptorAdapter {
	//业务处理完成，准备返回view前处理，可以修改modelandview
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		long starttime = (long) request.getAttribute("starttime");
		request.removeAttribute("starttime");
		long endtime = System.currentTimeMillis();
		System.out.println("本次请求处理时间为："+new Long(endtime-starttime));
	}
	//预处理
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		long starttime = System.currentTimeMillis();
		request.setAttribute("starttime", starttime);
		return true;
	}
	
}
